home *** CD-ROM | disk | FTP | other *** search
- Path: news.mel.aone.net.au!usenet
- From: clyde@hitech.com.au (Clyde Smith-Stubbs)
- Newsgroups: comp.lang.c
- Subject: Re: What the hell is THIS?! - afile (1/1)
- Date: Mon, 15 Jan 1996 22:14:13 GMT
- Organization: HI-TECH Software
- Message-ID: <30fad19c.85098432@news.bne.aone.net.au>
- References: <4d6rgh$rfu@abel.cc.sunysb.edu>
- Reply-To: clyde@hitech.com.au
- NNTP-Posting-Host: skyhawk.hitech.com.au
- X-Newsreader: Forte Agent .99c/16.141
-
- In 12 Jan 1996 23:40:01 GMT, bmadhusu@engws12.ic.sunysb.edu
- (Bommasamudram Madhusudan) wrote:
-
- >Can someone explain what
- >
- > int (*p)[3] is?????
- >
- > Is this an array of pointers to integers
- >
- > OR
- >
- > a pointer to an array of integers?? HELP!
-
- Well, in spite of what someone else said in the newsgroup, this is a
- pointer to an array of integers. There are two ways to figure out
- difficult declarations like this; the classical way is to work out
- what happens when you use the variable, remembering that the syntax of
- declarations is based on the syntax of expressions. So in an
- expression
-
- (*p)[0]
-
- will firstly dereference p, then index the result. This means p is a
- pointer to an array. The other way is to follow the following rules:
-
- Read the declaration's innermost parentheses first;
- Read from right to left.
-
- The declaration was:
-
- int (*p)[3];
-
- So in this case we first read inside the parens - start with 'p',
- "p is"; then read the '*', so "p is a pointer". Now step outside the
- parens, and go as far right as we can and start reading back to the
- left again:
-
- We read: "p * [3] int"
-
- "p is a pointer to a 3 element array of int".
-
- Now if the parens were omitted, then we would start from the array,
- and then p would be an array of pointers to int.
-
- Here's another example:
-
- const int * (*f)(int, char);
-
- Read this as:
-
- "f * (int, char) * 'const int'"
-
- or "f is a pointer to a function taking an int and a char returning a
- pointer to a const int". Where there's an adjacent sequence of
- keywords - data types, modifiers and qualifiers, the order is not
- important, i.e. "const int" is the same as "int const".
-
- As to why it crashes when you reference it - did you initialize the
- pointer to point to anything? I've attached a sample program that
- works just fine.
-
- Cheers.
-
-
- ----
- Clyde Smith-Stubbs | HI-TECH Software, | Voice: +61 7 3300 5011
- clyde@hitech.com.au | P.O. Box 103, Alderley, | Fax: +61 7 3300 5246
- http://www.hitech.com.au | QLD, 4051, AUSTRALIA. | BBS: +61 7 3300 5235
- ----------------------------------------------------------------------------
- FREE! Download our shareware (FREE for noncommercial use) MS-DOS C Compiler!
- Point your Web browser at http://www.hitech.com.au/
-
- begin 644 afile
- M(VEN8VQU9&4)/'-T9&QI8BYH/@T*(VEN8VQU9&4)/'-T9&EO+F@^#0IM86EN
- M*"D-"GL-"@EI;G0@*"IP*5LS73L-"@T*"7`@/2!M86QL;V,H<VEZ96]F("IP
- M*3L-"@T*"2@J<"E;,%T@/2`R,SL-"@EP<FEN=&8H(B@J<"E;,%T@/2`E9%QN
- 1(BP@*"IP*5LP72D[#0I]#0HV
- `
- end
-